package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"lin6xcom/src/models"
	"lin6xcom/src/utils"
	"log"
)

/*
文章业务相关控制
@update 2024/05/29
@author lin6xcom
*/

// 文章业务相关控制
type ArticleController struct{}

//增加文章--------

func (ArticleController) AddArticle(ctx *gin.Context) {
	//1.验证登录，此步骤已经使用中间件utils.Middleware.Auth拦截验证

	//添加文章
	article := models.Article{}
	article.ArticleId = uint(uuid.New().ID())
	error_a := ctx.BindJSON(&article)
	if error_a != nil {
		ctx.JSON(200, gin.H{
			"code":    200,
			"msg":     "参数错误",
			"success": false,
			"data":    nil,
		})
		fmt.Println(error_a)
		return
	}
	fmt.Println(article)
	if article.Title == "" {
		ctx.JSON(200, gin.H{
			"code":    200,
			"msg":     "标题不能为空",
			"success": false,
			"data":    nil,
		})
		return
	}
	if article.Content == "" {
		ctx.JSON(200, gin.H{
			"code":    200,
			"msg":     "内容不能为空",
			"success": false,
			"data":    nil,
		})
		return
	}
	if article.CategoryId == 0 {
		ctx.JSON(200, gin.H{
			"code":    200,
			"msg":     "分类不能为空",
			"success": false,
			"data":    nil,
		})
		return
	}
	if article.UserId == 0 {
		ctx.JSON(200, gin.H{
			"code":    200,
			"msg":     "用户id不能为空",
			"success": false,
			"data":    nil,
		})
		return
	}
	if article.ArticleImg == "" {
		ctx.JSON(200, gin.H{
			"code":    200,
			"msg":     "图片不能为空",
			"success": false,
			"data":    nil,
		})
		return
	}

	if article.Title != "" && article.Content != "" && article.CategoryId != 0 && article.UserId != 0 && article.ArticleImg != "" {
		db := utils.LinkDB()
		_, err := db.Insert(&article)
		if err != nil {
			log.Fatalf("Insert error: %v", err)

			return
		}
		ctx.JSON(200, gin.H{
			"code":    200,
			"msg":     "增加文章成功",
			"success": true,
			"data":    article,
		})
	}
}

//获取单个文章--------

func (ArticleController) GetArticle(ctx *gin.Context) {
	articleId, _ := ctx.GetQuery("articleId")
	fmt.Println(articleId)
	data := models.Article{}
	db := utils.LinkDB()
	get, _ := db.Where("article_id=?", articleId).Get(&data)
	if !get {
		ctx.JSON(200, gin.H{
			"code":    200,
			"msg":     "获取文章失败",
			"success": false,
			"data":    nil,
		})
		return
	}
	ctx.JSON(200, gin.H{
		"code":    200,
		"msg":     "获取文章成功",
		"success": true,
		"data":    data,
	})

}

// 获取 所有文章--------
func (ArticleController) GetAllArticle(ctx *gin.Context) {
	data := []models.Article{}
	db := utils.LinkDB()
	_ = db.Find(&data)
	ctx.JSON(200, gin.H{
		"code":    200,
		"msg":     "获取所有文章",
		"success": true,
		"data":    data,
	})

}

// 根据用户id获取所有文章--------
func (ArticleController) GetUserAllArticle(ctx *gin.Context) {
	userId, b := ctx.GetQuery("userId")
	if !b {
		ctx.JSON(200, gin.H{
			"code":    200,
			"msg":     "获取用户文章失败",
			"success": false,
			"data":    nil,
		})
		return
	}
	data := []models.Article{}
	db := utils.LinkDB()
	_ = db.Where("user_id=?", userId).Find(&data)
	ctx.JSON(200, gin.H{
		"code":    200,
		"msg":     "获取用户文章成功",
		"success": true,
		"data":    data,
	})
}

// 删除文章--------

func (ArticleController) DeleteArticle(ctx *gin.Context) {
	articleId, b := ctx.GetQuery("articleId")
	if articleId == "" || !b {
		ctx.JSON(200, gin.H{
			"code":    200,
			"msg":     "文章id不能为空",
			"success": false,
			"data":    nil,
		})
		return
	}
	db := utils.LinkDB()
	col, _ := db.Where("article_id=?", articleId).Delete(&models.Article{})
	if col < 1 {
		ctx.JSON(200, gin.H{
			"code":    200,
			"msg":     "删除文章失败,文章失效或不存在",
			"success": false,
			"data":    nil,
		})
		return
	}
	ctx.JSON(200, gin.H{
		"code":    200,
		"msg":     "删除文章成功",
		"success": true,
		"data":    nil,
	})

}

// 删除用户所有文章--------
// 此方法高危，暂时不用
func (ArticleController) DeleteUserAllArticle(ctx *gin.Context) {
	userId, b := ctx.GetQuery("userId")
	if !b {
		ctx.JSON(200, gin.H{
			"code":    200,
			"msg":     "用户id不能为空",
			"success": false,
			"data":    nil,
		})
		return
	}
	db := utils.LinkDB()
	col, _ := db.Where("user_id=?", userId).Delete(&models.Article{})
	if col < 1 {
		ctx.JSON(200, gin.H{
			"code":    200,
			"msg":     "删除用户文章失败,用户失效或不存在",
			"success": false,
			"data":    nil,
		})
		return
	}
	ctx.JSON(200, gin.H{
		"code":    200,
		"msg":     "删除用户所有文章成功",
		"success": true,
		"data":    nil,
	})
}

// 更新文章--------
// 此方法或需要稍微修改
func (ArticleController) UpdateArticle(ctx *gin.Context) {
	article := models.Article{}
	_ = ctx.BindJSON(&article)
	if article.Title == "" {
		ctx.JSON(200, gin.H{
			"code":    200,
			"msg":     "标题不能为空",
			"success": false,
			"data":    nil,
		})
		return
	}
	if article.Content == "" {
		ctx.JSON(200, gin.H{
			"code": 200,
			"msg":  "内容不能为空",
		})
	}
	if article.CategoryId == 0 {
		ctx.JSON(200, gin.H{
			"code":    200,
			"msg":     "分类不能为空",
			"success": false,
			"data":    nil,
		})
		return
	}
	if article.UserId == 0 {
		ctx.JSON(200, gin.H{
			"code":    200,
			"msg":     "用户id不能为空",
			"success": false,
			"data":    nil,
		})
		return
	}

	db := utils.LinkDB()
	col, _ := db.Where("article_id=?", article.ArticleId).Update(&article)
	if col < 1 {
		ctx.JSON(200, gin.H{
			"code":    200,
			"msg":     "更新文章失败,文章失效或不存在",
			"success": false,
			"data":    nil,
		})
		return
	}
	ctx.JSON(200, gin.H{
		"code":    200,
		"msg":     "更新文章成功",
		"success": true,
		"data":    nil,
	})
}
